<?php


namespace app\admin\controller\mall;


use app\admin\model\MallCollage;
use app\admin\model\MallGoods;
use app\admin\model\MallGoodsSpec;
use app\admin\model\MallOrder;
use app\admin\traits\Curd;
use app\common\controller\AdminController;
use EasyAdmin\annotation\ControllerAnnotation;
use EasyAdmin\annotation\NodeAnotation;
use jianyan\excel\Excel;
use think\App;

/**
 * Class Goods
 * @package app\admin\controller\mall
 * @ControllerAnnotation(title="统计订单")
 */
class Count extends AdminController
{
    
    /**
     * 允许修改的字段
     * @var array
     */
    protected $allowModifyFields = [
        'status',
        'sort',
        'remark',
        'is_top',
        'is_novices_exclusive',
        'title',
    ];
    public function __construct(App $app)
    {
        parent::__construct($app);
        $this->model = new MallGoods();
    }
    
    /**
     * @NodeAnotation(title="列表")
     */
    public function order()
    {
        if ($this->request->isAjax()) {
            if (input('selectFields')) {
                return $this->selectList();
            }
            [$page, $limit, $where] = $this->buildTableParames();
            $where[]     = ['order_status', 'in', [10, 30, 40, 50, 60]];
            $where[]     = ['user.is_virtual', '=', 0];
            $count       = MallOrder::withJoin(['user'])
                ->where($where)
                ->group('user_id')
                ->count();
            $list        = MallOrder::withJoin(['user'])
                ->where($where)
                ->group('user_id')
                ->field('count(1) as order_num,SUM(order_price) as order_price')
                ->page($page, $limit)
                ->order($this->sort)
                ->select()
                ->toArray();
            $userIds     = array_column($list, 'user_id');
            $where       = [['uid', 'in', $userIds]];
            $where[]     = ['order_status', 'in', [10, 30, 40, 50, 60, 70]];
            $collageIds  = MallOrder::where($where)
                ->column('u_collage_id');
            $collageIds1 = MallCollage::where([['collage_id', 'in', $collageIds], ['finish_time', '=', null]])->column('collage_id');
            $collageIds2 = MallCollage::where([['collage_id', 'in', $collageIds], ['finish_time', '>', 0]])->column('collage_id');
            $orderIds1   = MallOrder::where([['u_collage_id', 'in', $collageIds1], ['order_status', 'in', [10, 30, 40, 50, 60]]])->column('id');
            $orderIds2   = MallOrder::where([['u_collage_id', 'in', $collageIds2], ['order_status', 'in', [10, 30, 40, 50, 60, 70]]])->column('id');
            $sum         = MallOrder::where([['id', 'in', array_merge($orderIds1, $orderIds2)]])
                ->field('count(1) as count,SUM(order_price) as total_price,uid')
                ->group('uid')
                ->select()
                ->toArray();
            $sum         = array_column($sum, null, 'uid');
            foreach ($list as $key => $value) {
                $list[ $key ][ 'invite_num' ]   = $sum[ $value[ 'user_id' ] ][ 'count' ] ?? 0;
                $list[ $key ][ 'invite_price' ] = $sum[ $value[ 'user_id' ] ][ 'total_price' ] ?? 0;
            }
            $data = [
                'code'  => 0,
                'msg'   => '',
                'count' => $count,
                'data'  => $list,
            ];
            return json($data);
        }
        return $this->fetch();
    }
    /**
     * @NodeAnotation(title="拼团订单")
     */
    public function detail()
    {
        if ($this->request->isAjax()) {
            if (input('selectFields')) {
                return $this->selectList();
            }
            [$page, $limit, $where] = $this->buildTableParames();
            $userId  = MallOrder::where('id', input('id'))->value('user_id');
            $where[] = ['mall_order.user_id', '=', $userId];
            $where[] = ['order_status', 'in', [10, 30, 40, 50, 60]];
            $count   = MallOrder::withJoin(['user', 'goods', 'collage'], 'LEFT')
                ->where($where)
                ->count();
            $list    = MallOrder::withJoin(['user', 'goods', 'collage'], 'LEFT')
                ->where($where)
                ->page($page, $limit)
                ->order($this->sort)
                ->select()
                ->toArray();
            $data    = [
                'code'  => 0,
                'msg'   => '',
                'count' => $count,
                'data'  => $list,
            ];
            return json($data);
        }
        return $this->fetch();
    }
//    /**
//     * @NodeAnotation(title="导出")
//     */
//    public function export()
//    {
//        [$page, $limit, $where] = $this->buildTableParames();
//        $header  = [
//            ['用户ID', 'user_id'],
//            ['用户名', 'user_name'],
//            ['订单数量', 'order_num'],
//            ['订单金额', 'order_price'],
//            ['邀约人数', 'invite_num'],
//            ['邀约金额', 'invite_price'],
//        ];
//        $where[] = ['order_status', 'in', [10, 30, 50, 60]];
//        $where[] = ['user.is_virtual', '=', 0];
//        $count   = MallOrder::withJoin(['user'], 'LEFT')
//            ->where($where)
//            ->group('user_id')
//            ->count();
//        $list    = MallOrder::withJoin(['user'], 'LEFT')
//            ->where($where)
//            ->group('user_id')
//            ->field('count(1) as order_num,SUM(order_price) as order_price')
//            ->page($page, $limit)
//            ->order($this->sort)
//            ->select()
//            ->toArray();
//        $userIds = array_column($list, 'user_id');
//        $where[] = ['uid', 'in', $userIds];
//        $sum     = MallOrder::withJoin(['user'])
//            ->where($where)
//            ->field('count(1) as count,SUM(order_price) as total_price,uid')
//            ->group('uid')
//            ->select()
//            ->toArray();
//        $sum     = array_column($sum, null, 'uid');
//        foreach ($list as $key => $value) {
//            $list[ $key ][ 'invite_num' ]   = $sum[ $value[ 'user_id' ] ][ 'count' ] ?? 0;
//            $totalPrice                     = $sum[ $value[ 'user_id' ] ][ 'total_price' ] ?? 0;
//            $list[ $key ][ 'invite_price' ] = '￥' . $totalPrice / 100 ?? 0;
//            $list[ $key ][ 'user_name' ]    = $value[ 'user' ][ 'name' ] ?? '';
//
//        }
//        $fileName = '统计订单' . time();
//        return Excel::exportData($list, $header, $fileName, 'xlsx');
//    }
    
}